/**
 * Created : Mar 27, 2012
 *
 * @author pquiring
 */

import java.awt.*;
import java.util.*;
import javax.swing.*;

public class CalculatorApp extends javax.swing.JFrame {

  public static String version = "0.4";

  /**
   * Creates new form CalculatorApp
   */
  public CalculatorApp() {
    initComponents();
    backend = new Backend();
    loadPanel(0);
    setPosition();
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menuBar = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        cut = new javax.swing.JMenuItem();
        copy = new javax.swing.JMenuItem();
        paste = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        viewFormula = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        quit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        basicMode = new javax.swing.JMenuItem();
        sciMode = new javax.swing.JMenuItem();
        devMode = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        help = new javax.swing.JMenuItem();
        about = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("jCalculator");
        setResizable(false);

        jMenu1.setText("Edit");

        cut.setText("Cut");
        cut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cutActionPerformed(evt);
            }
        });
        jMenu1.add(cut);

        copy.setText("Copy");
        copy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                copyActionPerformed(evt);
            }
        });
        jMenu1.add(copy);

        paste.setText("Paste");
        paste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pasteActionPerformed(evt);
            }
        });
        jMenu1.add(paste);
        jMenu1.add(jSeparator1);

        viewFormula.setText("View Formula");
        viewFormula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewFormulaActionPerformed(evt);
            }
        });
        jMenu1.add(viewFormula);
        jMenu1.add(jSeparator2);

        quit.setText("Quit");
        quit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitActionPerformed(evt);
            }
        });
        jMenu1.add(quit);

        menuBar.add(jMenu1);

        jMenu2.setText("Mode");

        basicMode.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
        basicMode.setText("Basic");
        basicMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                basicModeActionPerformed(evt);
            }
        });
        jMenu2.add(basicMode);

        sciMode.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, 0));
        sciMode.setText("Scientific");
        sciMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sciModeActionPerformed(evt);
            }
        });
        jMenu2.add(sciMode);

        devMode.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, 0));
        devMode.setText("Programmer");
        devMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                devModeActionPerformed(evt);
            }
        });
        jMenu2.add(devMode);

        menuBar.add(jMenu2);

        jMenu3.setText("Help");

        help.setText("Help");
        help.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpActionPerformed(evt);
            }
        });
        jMenu3.add(help);

        about.setText("About");
        about.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutActionPerformed(evt);
            }
        });
        jMenu3.add(about);

        menuBar.add(jMenu3);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 273, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

  private void viewFormulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewFormulaActionPerformed
    backend.endEntry();
    FormulaDialog dialog = new FormulaDialog(this, true, backend);
    backend.inDialog = true;
    dialog.setVisible(true);
    backend.inDialog = false;
    if (backend.formula.get(backend.fidx).length() > 0) {
      backend.formula.add("");
      backend.fidx++;
    }
    backend.display.setDisplay("");
  }//GEN-LAST:event_viewFormulaActionPerformed

  private void quitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitActionPerformed
    System.exit(0);
  }//GEN-LAST:event_quitActionPerformed

  private void basicModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_basicModeActionPerformed
    loadPanel(0);
  }//GEN-LAST:event_basicModeActionPerformed

  private void sciModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sciModeActionPerformed
    loadPanel(1);
  }//GEN-LAST:event_sciModeActionPerformed

  private void devModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_devModeActionPerformed
    loadPanel(2);
  }//GEN-LAST:event_devModeActionPerformed

  private void cutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cutActionPerformed
    backend.display.cut();
  }//GEN-LAST:event_cutActionPerformed

  private void copyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyActionPerformed
    backend.display.copy();
  }//GEN-LAST:event_copyActionPerformed

  private void pasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pasteActionPerformed
    backend.paste();
  }//GEN-LAST:event_pasteActionPerformed

  private void aboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutActionPerformed
    backend.about();
  }//GEN-LAST:event_aboutActionPerformed

  private void helpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpActionPerformed
    try {
      Runtime.getRuntime().exec("jhelp jfcalc");
    } catch (Exception e) {
    }
  }//GEN-LAST:event_helpActionPerformed

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    /*
     * Create and display the form
     */
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new CalculatorApp().setVisible(true);
      }
    });
  }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem about;
    private javax.swing.JMenuItem basicMode;
    private javax.swing.JMenuItem copy;
    private javax.swing.JMenuItem cut;
    private javax.swing.JMenuItem devMode;
    private javax.swing.JMenuItem help;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem paste;
    private javax.swing.JMenuItem quit;
    private javax.swing.JMenuItem sciMode;
    private javax.swing.JMenuItem viewFormula;
    // End of variables declaration//GEN-END:variables

  private Backend backend;

  private void setFrameSize(Dimension size) {
    Insets insets = getInsets();
    size.width += insets.left + insets.right;
    size.height += insets.top + insets.bottom;
    size.height += menuBar.getHeight();
    setSize(size);
  }

  private void loadPanel(int type) {
    backend.mode = type;
    JPanel panel = null;
    switch (type) {
      case 0:  //basic
        panel = new BasicPanel(backend);
        break;
      case 1:  //scientific
        panel = new ScientificPanel(backend);
        break;
      case 2:  //programmer
        panel = new ProgrammerPanel(backend);
        break;
    }
    setContentPane(panel);
    setFrameSize(panel.getPreferredSize());
    backend.display = (Display)panel;
    backend.doAllClear();
    validate();
    repaint();
  }
  private void setPosition() {
    Rectangle s = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
    Dimension d = getSize();
    setLocation(s.width/2 - d.width/2, s.height/2 - (d.height/2));
  }
}
